﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DigitalRubyShared;
using DG.Tweening;
public class Level2Clock : MonoBehaviour
{
    public GameLevelItem gameLevelItem;
    public Text loveText;

    public GameObject rotateScaleObject;
    public GameObject rotateScaleRoot;
    private OneTouchRotateGestureRecognizer rotationGestureScale = new OneTouchRotateGestureRecognizer();
    private float totalAngleScale;


    public GameObject rotateAlphaObject;
    public GameObject rotateAlphaRoot;
    private OneTouchRotateGestureRecognizer rotationGestureAlpha = new OneTouchRotateGestureRecognizer();
    private float totalAngleAlpha;

    private float rateScale = 0;
    private float rateAlpha = 0;
    private bool result = false;

    private void OnEnable()
    {
        FingersScript.Instance.AddGesture(rotationGestureScale);
        rotationGestureScale.StateUpdated += RotationGestureScaleUpdated;
        rotationGestureScale.PlatformSpecificView = rotateScaleObject;

        FingersScript.Instance.AddGesture(rotationGestureAlpha);
        rotationGestureAlpha.StateUpdated += RotationGestureAlphaUpdated;
        rotationGestureAlpha.PlatformSpecificView = rotateAlphaObject;
    }

    private void OnDisable()
    {
        FingersScript.Instance.RemoveGesture(rotationGestureScale);
        FingersScript.Instance.RemoveGesture(rotationGestureAlpha);
    }

    private void RotationGestureScaleUpdated(DigitalRubyShared.GestureRecognizer gesture)
    {
        if (gesture.State == GestureRecognizerState.Executing)
        {
            if (result)
                return;
            //Debug.Log(rotationGesture.RotationDegreesDelta);
            if (rotationGestureScale.RotationDegreesDelta < 0)
            {
                totalAngleScale += rotationGestureScale.RotationDegreesDelta;
                //Debug.Log(totalAngle);
                rotateScaleRoot.transform.Rotate(0.0f, 0.0f, rotationGestureScale.RotationDegreesDelta);
                float angle = totalAngleScale % 360f;
                angle = Mathf.Abs(angle);
                rateScale = angle / 360f;
                loveText.transform.localScale = Vector3.one * rateScale;
                JudgeResult();
            }

        }
    }

    private void RotationGestureAlphaUpdated(DigitalRubyShared.GestureRecognizer gesture)
    {
        if (result)
            return;
        if (gesture.State == GestureRecognizerState.Executing)
        {
            //Debug.Log(rotationGesture.RotationDegreesDelta);
            if (rotationGestureAlpha.RotationDegreesDelta < 0)
            {
                totalAngleAlpha += rotationGestureAlpha.RotationDegreesDelta;
                //Debug.Log(totalAngle);
                rotateAlphaRoot.transform.Rotate(0.0f, 0.0f, rotationGestureAlpha.RotationDegreesDelta);
                float angle = totalAngleAlpha % 360;
                angle = Mathf.Abs(angle);
                rateAlpha = angle / 360.0f;
                loveText.DOFade(rateAlpha, 0.1f); JudgeResult();
            }
        }
    }

    private void JudgeResult()
    {
        if (rateScale > 0.95f && rateAlpha > 0.95)
        {
            result = true;
            gameLevelItem.TriggerResult(true);
            gameLevelItem.TriggerResult(true);
        }
    }
}

